#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    m_seriaPort = new QSerialPort();
    model=new QStandardItemModel(ui->treeView);
    ui->treeView->setModel(model);
    initUI();
    m_portNameList = getPortNameList();
    ui->comboBox->addItems(m_portNameList);
    connect(ui->pushButton,&QPushButton::clicked,this,&Widget::openPort);
    //connect(ui->pushButton_2,&QPushButton::clicked,this,&Widget::sendInfo);

}

Widget::~Widget()
{
    if(m_seriaPort->isOpen()){
        m_seriaPort->close();
    }
    delete m_seriaPort;
    delete ui;
}

void Widget::initUI(){
    this->setWindowTitle("郑鑫");
    model->setHorizontalHeaderLabels(QStringList()<<QStringLiteral("类型")<<QStringLiteral("网络地址")<<QStringLiteral("数值"));
    ui->treeView->header()->resizeSection(0,150);
    ui->treeView->header()->resizeSection(1,70);
    ui->treeView->header()->resizeSection(2,50);
    m_publicIconMap[QStringLiteral("router")]=QIcon(QStringLiteral(":/router.png"));
    m_publicIconMap[QStringLiteral("coor")]=QIcon(QStringLiteral(":/coor.png"));
    m_publicIconMap[QStringLiteral("end")]=QIcon(QStringLiteral(":/end.png"));
}

void Widget::openPort(){ //打开按钮的处理事件
    if(m_seriaPort->isOpen()){
        m_seriaPort->clear();
        m_seriaPort->close();
        ui->pushButton->setText("open");
    }else{

    m_seriaPort->setPortName(ui->comboBox->currentText());

    if(!m_seriaPort->open(QIODevice::ReadWrite)){
        qDebug()<<"打开失败!";
        return;
    }
    qDebug()<<"串口打开成功!";
    m_seriaPort->setBaudRate(QSerialPort::Baud38400,QSerialPort::AllDirections);
    m_seriaPort->setDataBits(QSerialPort::Data8);
    m_seriaPort->setFlowControl(QSerialPort::NoFlowControl);
    m_seriaPort->setParity(QSerialPort::NoParity);
    m_seriaPort->setStopBits(QSerialPort::OneStop);

    connect(m_seriaPort,SIGNAL(readyRead()),this,SLOT(receiveInfo()));
    ui->pushButton->setText("close");
    }
}

void Widget::receiveInfo(){//串口的接受代码
    QByteArray info = m_seriaPort->readAll();
    if(info.size()==8){
        infoBuf.clear();
        infoBuf.append(info);
    }else{
        infoBuf.append(info);  //拿到了15个字节的完整包
        QString str = QString(infoBuf);
    }
//    if(!info.isEmpty() && coorFlag == 0){
//        rxbf.clear();
//        RXBF rx;
//        RXBFather rxf;
//        if(model->rowCount()>0)
//            model->removeRow(0);
//        rx.pNWK = 0;
//        rx.myNWK = 0;
//        rx.value = 0;
//        QStandardItem* itemCoor=new QStandardItem(m_publicIconMap[QStringLiteral("coor")],QStringLiteral("协调器"));
//        rxf.myNWK = 0;
//        rxf.item = itemCoor;
//        rxbfather.push_back(rxf);
//        model->appendRow(itemCoor);
//        model->setItem(model->indexFromItem(itemCoor).row(),1,new QStandardItem("0x0000"));
//        model->setItem(model->indexFromItem(itemCoor).row(),2,new QStandardItem("无"));
//        //disZigbee(0,itemCoor);
//        coorFlag++;
//    }else if(0<info.at(4)){//传感器数据
//        RXBF rxEnd;
//        rxEnd.myNWK=info.at(4);
//        rxEnd.value=info.at(10);
//        rxEnd.pNWK=info.at(12);
//        disZigbee(rxEnd);
//    }
}

void Widget::disZigbee(RXBF rxEnd){//建立树的代码
    RXBFather rxf;
    if(rxEnd.myNWK<9){ //路由
        QStandardItem* childRouItem = new QStandardItem(m_publicIconMap[QStringLiteral("router")],QStringLiteral("路由器"));;
        rxf.myNWK = rxEnd.myNWK;
        rxf.item = childRouItem;
        rxbfather.push_back(rxf);
        QStandardItem* fatherItem = rxbfather.begin()->item;
        fatherItem->appendRow(childRouItem);
        fatherItem->setChild(fatherItem->rowCount()-1,1,new QStandardItem("0x"+QString::number(rxEnd.myNWK,16)));
        fatherItem->setChild(fatherItem->rowCount()-1,2,new QStandardItem("0x"+QString::number(rxEnd.value,16)));
    }else{//终端
        QStandardItem* childEndItem = new QStandardItem(m_publicIconMap[QStringLiteral("end")],QStringLiteral("终端"));;
        for(int i=0; i<rxbfather.size();i++){
            if(rxEnd.pNWK == rxbfather[i].myNWK){
                QStandardItem* fatherItem = rxbfather[i].item;
                fatherItem->appendRow(childEndItem);
                fatherItem->setChild(fatherItem->rowCount()-1,1,new QStandardItem("0x"+QString::number(rxEnd.myNWK,16)));
                fatherItem->setChild(fatherItem->rowCount()-1,2,new QStandardItem("0x"+QString::number(rxEnd.value,16)));
                break;
            }
        }
    }
}

QStringList Widget::getPortNameList(){//获取可用的串口
    QStringList portname;
    foreach(const QSerialPortInfo &info,QSerialPortInfo::availablePorts()){
        portname<<info.portName();
        qDebug()<<"serialPortName:"<<info.portName();
    }
    return portname;
}



